﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.COMMON.Core;
using Twi.Pages.SCS.Model;

namespace Twi.Pages.SCS.BLL
{
    public class _User
    {
        private static readonly _User instance = new _User();
        private _User() { }
        public static _User Instance
        {
            get { return instance; }
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public List<TUser> GetAllList()
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TUser
                        orderby p.FRealName
                        select p;
                return q.ToList();
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public TUser LoginIn(string userName, string pwd)
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TUser
                        where p.FName == userName && p.FPwd == pwd
                        select p;
                TUser user = q.FirstOrDefault();
                if (user == null)
                {
                    throw new Exception("用户名或密码错误");
                }
                else
                {
                    return user;
                }
            }
        }
    }
}
